class StudentInfoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def securityService
    def postService
 
    def show = {
        def currentUser = securityService.getCurrentUser()
        def studentInfoInstance = Student.get(currentUser.id)?.studentInfo
        
        return [studentInfoInstance: studentInfoInstance, author: currentUser, site:Site.findByAuthor(currentUser),classeSites:postService.getClassSiteByCurrentUserClassId(currentUser)]
    }

    def edit = {
        def currentUser = securityService.getCurrentUser()
        def studentInfoInstance = Student.get(currentUser.id)?.studentInfo
        
        return [studentInfoInstance: studentInfoInstance, author: currentUser, site:Site.findByAuthor(currentUser),classeSites:postService.getClassSiteByCurrentUserClassId(currentUser)]
    }

    def update = {
    log.info("111111111111")
        def currentUser = securityService.getCurrentUser()
	def studentInfoInstance
        if(!params.id){
		    studentInfoInstance = new StudentInfo(params)
        }else{
            studentInfoInstance = StudentInfo.get(params.id)
            studentInfoInstance.properties = params
	    }
        // todo  后面将要改成session
        Student s = Student.get(currentUser.id)
        if (studentInfoInstance.save(flush: true)) {
            log.info("22222222222")
            flash.message = "信息修改成功"
            s.studentInfo = studentInfoInstance
            s.save();
            redirect(action: "show")
        }
        else {
        log.info("33333333333")
            render(view: "edit", model: [studentInfoInstance: studentInfoInstance, author: currentUser, site:Site.findByAuthor(currentUser),classeSites:postService.getClassSiteByCurrentUserClassId(currentUser)])
        }
    }
 
}
